// @ts-nocheck
import OSS from 'ali-oss';
import dayjs from 'dayjs';

import { request } from '@/utils/request';

export function post(path: string, data: Record<string, unknown>) {
  return request.post<Record<string, unknown>>({
    url: path,
    data,
  });
}

export function saveOrUpdate(path: string, data: Record<string, unknown>) {
  return post(path, data);
}

export async function batchUpdateAliyun(objId: number, objType: number, objTypeStr: string, files: any[]) {
  console.log('files', files);

  const res = await post('/aliyun/ststoken/get', {});

  const alioss = new OSS({
    region: res.region,
    accessKeyId: res.accessKeyId,
    accessKeySecret: res.accessKeySecret,
    stsToken: res.securityToken,
    bucket: res.bucket,
  });

  for (const file of files) {
    const filePath = `${objTypeStr}/${dayjs().format('YYYY/MM/DD')}/${crypto.randomUUID().replaceAll('-', '') + file.name.substring(file.name.indexOf('.'))}`;
    // eslint-disable-next-line no-await-in-loop
    await alioss.put(filePath, file.raw);

    // eslint-disable-next-line no-await-in-loop
    await post('/aliyun/attachmentUpload', {
      filePath,
      objType,
      objId,
      fileSuffix: file.name.substring(file.name.indexOf('.') + 1),
      fileSize: file.size,
      originalFileName: file.name,
      fileType: file.type,
    });
  }
}
